/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tvunetworks.dao.ibatis;

import com.tvunetworks.dao.OrderDao;
import com.tvunetworks.model.LineItem;
import com.tvunetworks.model.Order;
import java.util.List;
import org.springframework.orm.ObjectRetrievalFailureException;

/**
 *
 * @author michael
 */
public class OrderDaoiBatis extends GenericDaoiBatis<Order, Integer> implements OrderDao {

    public OrderDaoiBatis() {
        super(com.tvunetworks.model.Order.class);
    }

    public Order getOrder(Integer orderId) {
        Order o = (Order) getSqlMapClientTemplate().queryForObject("getOrder", orderId);
        if (o == null) {
            logger.warn("no order found...");
            throw new ObjectRetrievalFailureException(Order.class, orderId);
        }
        o.setLineItems(getSqlMapClientTemplate().queryForList("getLineItemsByOrderId", orderId));
        return o;
    }

    public List<Order> getOrdersByUsername(String username) {
        return getSqlMapClientTemplate().queryForList("getOrdersByUsername", username);
    }

    public void insertOrder(Order order) {
        getSqlMapClientTemplate().insert("insertOrder", order);
        getSqlMapClientTemplate().insert("insertOrderStatus", order);

        for (LineItem item : order.getLineItems()) {
            getSqlMapClientTemplate().insert("insertLineItem", item);
        }
    }
}
